﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyMeta;
namespace Liger.Generate
{
    public class ViewBase
    {
        #region 标示列
        public bool HasIdentityColumn
        {
            get
            {
                foreach (var column in Columns)
                {
                    if (column.IsAutoKey)
                        return true;
                }
                return false;
            }
        }
        public ColumnBase IdentityColumn
        {
            get
            {
                foreach (var column in Columns)
                {
                    if (column.IsAutoKey)
                        return column;
                }
                return new ColumnBase(new Column());
            }
        }

        public string ClassName
        {
            get
            {
                return this.Name.Replace(" ", "").Replace("_", "").Trim();
            }
        }
        #endregion

        #region 主键

        public bool HasPrimaryKey
        {
            get
            {
                foreach (var column in Columns)
                {
                    if (column.IsInPrimaryKey)
                        return true;
                }
                return false;
            }
        }
        public List<ColumnBase> PrimaryKeyColumns
        {
            get
            {
                var list = new List<ColumnBase>();
                foreach (var column in Columns)
                {
                    if (column.IsInPrimaryKey)
                        list.Add(column);
                }
                return list;
            }
        }
        #endregion


        public string Alias { get { return view.Alias; } }
        public List<ColumnBase> Columns { get; private set; }

        public string Description { get { return view.Description; } }

        public string Name { get { return view.Name; } }

        public string Schema { get { return view.Schema; } }

        public string Type { get { return view.Type; } }
        public string UserDataXPath { get { return view.UserDataXPath; } }
        public string ViewText { get { return view.ViewText; } }


        IView view;

        public ViewBase(IView view)
        {
            this.view = view;
            var list = new List<ColumnBase>();
            foreach (IColumn column in view.Columns)
            {
                list.Add(new ColumnBase(column));
            }
            this.Columns = list;
        }

    }
}